package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.CmsSourceMapper;
import com.originskyseed.platform.cms.model.CmsSource;
import com.originskyseed.platform.cms.service.ICmsSourceService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;

/**
 * 内容来源接口实现类
 *
 * @author LiangMingHui
 *
 */
@DubboService
@Component("cmsSourceService")
public class CmsSourceServiceImpl implements ICmsSourceService {

	@Resource
	private CmsSourceMapper cmsSourceMapper;

	/**
	 * deleteByPrimaryKey
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.cmsSourceMapper.deleteByPrimaryKey(id);
	}

	/**
	 * deleteByPrimaryKeys
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.cmsSourceMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * insert
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(CmsSource record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.cmsSourceMapper.insert(record);
	}

	/**
	 * insertSelective
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(CmsSource record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			record.setDelFlag(0);
		}
		record.setCreateTime(new Date());
		return this.cmsSourceMapper.insertSelective(record);
	}

	/**
	 * selectByPrimaryKey
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public CmsSource selectByPrimaryKey(String id) throws Exception {
		return this.cmsSourceMapper.selectByPrimaryKey(id);
	}

	@Override
	public CmsSource selectByNameCn(Map<String, Object> params) throws Exception {
		return cmsSourceMapper.selectByNameCn(params);
	}

	/**
	 * updateByPrimaryKeySelective
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(CmsSource record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsSourceMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * updateByPrimaryKey
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(CmsSource record) throws Exception {
		record.setUpdateTime(new Date());
		return this.cmsSourceMapper.updateByPrimaryKey(record);
	}

	/**
	 * 分页查询内容列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<CmsSource> selectListByPage(Map<String, Object> params, BasePage<CmsSource> page) throws Exception {
		params.put("page", page);
		List<CmsSource> list = this.cmsSourceMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 是否存在来源名称
	 *
	 * @param cmsSource
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean existSourceNameCn(CmsSource cmsSource) throws Exception {
		return cmsSourceMapper.existSourceNameCn(cmsSource);
	}

	/**
	 * 查询所有内容来源
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<CmsSource> selectAll(Map<String, Object> params) throws Exception {
		return this.cmsSourceMapper.selectAll(params);
	}

}
